package ex1;

import java.util.ResourceBundle;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
//import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;

public class ProfileForm extends ActionForm {

    private String firstName = null;
    private String lastName = null;
    private String favoriteColor = null;
    private String birthdate = null;
    private String adress = null;
    private String phone = null;

    public String getAdress() {
        return adress;
    }

    public String getBirthdate() {
        return birthdate;
    }

    public String getFavoriteColor() {
        return favoriteColor;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getPhone() {
        return phone;
    }

    public void setAdress(String adress) {
        this.adress = adress;
    }

    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }

    public void setFavoriteColor(String favoriteColor) {
        this.favoriteColor = favoriteColor;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public void reset(ActionMapping mapping,
                      HttpServletRequest request) {
        this.firstName = null;
        this.lastName = null;
        this.favoriteColor = null;
        this.birthdate = null;
        this.adress = null;
        this.phone = null;
    }

    @Override
    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {
        ResourceBundle rb = ResourceBundle.getBundle("ex1.ApplicationResources", request.getLocale());

        ActionErrors erreurs = new ActionErrors();

        if ((firstName == null) || (firstName.length() == 0)) {
            erreurs.add("firstName", new ActionMessage("error.firstName.required"));
        }
        if ((lastName == null) || (lastName.length() == 0)) {
            erreurs.add("lastName", new ActionMessage("error.lastName.required"));
        }
        if ((favoriteColor == null) || (favoriteColor.length() == 0)) {
            erreurs.add("favoriteColor", new ActionMessage("error.favoriteColor.required"));
        }
        else if (!rb.getString("valid.favoriteColor").equals(favoriteColor)) {
            erreurs.add("favoriteColor", new ActionMessage("error.favoriteColor.mustBeBlue"));
        }
        if ((birthdate == null) || (birthdate.length() == 0)) {
            erreurs.add("birthdate", new ActionMessage("error.birthdate.required"));
        }
        else if (!birthdate.matches("\\d+{1,2}/\\d{1,2}/\\d{2}")) {
            erreurs.add("birthdate", new ActionMessage("error.birthdate.invalid"));
        }
        if ((adress == null) || (adress.length() == 0)) {
            erreurs.add("adress", new ActionMessage("error.adress.required"));
        }
        if ((phone == null) || (phone.length() == 0)) {
            erreurs.add("phone", new ActionMessage("error.phone.required"));
        }
        else if (!phone.matches("\\d{10}")) {
            erreurs.add("phone", new ActionMessage("error.phone.invalid"));
        }

        return erreurs;
    }
}